package biz

import (
	"context"

	v1 "realworld/api/realworld/v1"

	"github.com/go-kratos/kratos/v2/errors"
	"github.com/go-kratos/kratos/v2/log"
)

var (
	// ErrUserNotFound is user not found.
	ErrUserNotFound = errors.NotFound(v1.ErrorReason_USER_NOT_FOUND.String(), "user not found")
)

// Greeter is a Greeter model.
type Greeter struct {
	Hello string
}
type Registration struct {
	Email    string
	Password string
}

// User 是用户模型
type User struct {
	ID       int64  `gorm:"primaryKey;autoIncrement"`
	Email    string `gorm:"column:email"`
	Password string `gorm:"column:password"`
	Token    string `gorm:"-"`
	Username string `gorm:"-"`
	Bio      string `gorm:"-"`
	Image    string `gorm:"-"`
}

// TableName 设置User的表名为`t_user`
func (User) TableName() string {
	return "t_user"
}

// GreeterRepo is a Greater repo.
type GreeterRepo interface {
	Save(context.Context, *Greeter) (*Greeter, error)
	Update(context.Context, *Greeter) (*Greeter, error)
	FindByID(context.Context, int64) (*Greeter, error)
	ListByHello(context.Context, string) ([]*Greeter, error)
	ListAll(context.Context) ([]*Greeter, error)
}

type UserRepo interface {
	Registration(context.Context, *Registration) (*User, error)
}

// GreeterUsecase is a Greeter usecase.
type GreeterUsecase struct {
	repo GreeterRepo
	uu   UserRepo
	log  *log.Helper
}

// NewGreeterUsecase new a Greeter usecase.
func NewGreeterUsecase(repo GreeterRepo, uu UserRepo, logger log.Logger) *GreeterUsecase {
	return &GreeterUsecase{repo: repo, uu: uu, log: log.NewHelper(logger)}
}

// CreateGreeter creates a Greeter, and returns the new Greeter.
func (uc *GreeterUsecase) CreateGreeter(ctx context.Context, g *Greeter) (*Greeter, error) {
	uc.log.WithContext(ctx).Infof("CreateGreeter: %v", g.Hello)
	return uc.repo.Save(ctx, g)
}

func (uc *GreeterUsecase) RegistrationInterface(ctx context.Context, r *Registration) (*User, error) {
	uc.log.WithContext(ctx).Infof("CreateGreeter: %v %v", r.Email, r.Password)
	return uc.uu.Registration(ctx, r)
}
